package com.ls.fw.office.poi.excel.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.ls.fw.office.poi.excel.bean.DimensionedImageType;
import com.ls.fw.office.poi.excel.bean.ExcelPictureData;
import com.ls.fw.office.poi.excel.support.mapper.FieldMapper;


public interface IExcelHandler {

	/**
	 * 
	 */
	public void init_sytle();
	
	/**
	 * 获取单元格中的值
	 * @param rowIndex 0开始
	 * @param colIndex 0开始
	 * @return
	 */
	public Object getCallValue(int rowIndex, int colIndex);
	
	public Object getCallValue(Sheet sheet,int rowIndex, int colIndex);
	
	
	/**
	 * 获取单元格中的值
	 * @param cellNumber for example "A1", "A2"
     *                   etc.
	 * @return
	 */
	public Object getCallValue(String cellNumber);
	
	public Object getCallValue(Sheet sheet,String cellNumber);
	
	public Object getCellFormatValue(Cell cell);
	
	/**
	 * 获取当前工作sheet
	 * @return
	 */
	public Sheet getSheet();
	
	
	/**
	 * 打开sheet
	 * @param num 0开始
	 * @return
	 */
	public Sheet switchSheet(int num);
	
	
	/**
	 * 获取一行数据
	 * @param rowIndex 0开始 
	 * @param startColIndex 0开始
	 * @param endColIndex 0开始
	 * @return
	 */
	public List<Object> getRow(int rowIndex,int startColIndex,int endColIndex);
	
	public List<Object> getRow(Sheet sheet,int rowIndex,int startColIndex,int endColIndex);
	
	
	/**
	 * 获取一行数据
	 * @param rowIndex 0开始
	 * @return
	 */
	public List<Object> getRow(int rowIndex);
	
	public List<Object> getRow(Sheet sheet,int rowIndex);
	
	/**
	 * 获取一列数据
	 * @param colIndex 0开始
	 * @param startRowIndex 0开始
	 * @param endRowIndex 0开始 
	 * @return
	 */
	public List<Object> getCol(int colIndex,int startRowIndex,int endRowIndex);
	
	public List<Object> getCol(Sheet sheet,int colIndex,int startRowIndex,int endRowIndex);
	
	/**
	 *  获取一列数据
	 * @param colIndex 0开始
	 * @return
	 */
	public List<Object> getCol(int colIndex);

	
	public List<Object> getCol(Sheet sheet,int colIndex);
	
	/**
	 * 获取图片
	 * @param excelPath
	 * @return
	 * @throws InvalidFormatException
	 * @throws IOException
	 */
	public List<ExcelPictureData> getAllPictures() throws InvalidFormatException, IOException;
	
	
	public String getVersion();
	
	/**
	 * 创建sheet
	 * @param name
	 * @return
	 */
	public Sheet createSheet(String name);
	
	/**
	 * 保存excel文件
	 * @throws IOException
	 */
	public void save() throws IOException;
	
	public void save(String filePath) throws IOException;
	
	public void save(OutputStream os) throws IOException;
	
    /**
     * Add an image to a worksheet.
     *
     * @param colNumber A primitive int that contains the index number of a
     *                  column on the worksheet; POI column indices are zero
     *                  based. Together with the rowNumber parameter's value,
     *                  this parameter identifies a cell on the worksheet. The
     *                  images top left hand corner will be aligned with the
     *                  top left hand corner of this cell.
     * @param rowNumber A primitive int that contains the index number of a row
     *                  on the worksheet; POI row indices are zero based.
     *                  Together with the rowNumber parameter's value, this
     *                  parameter identifies a cell on the worksheet. The
     *                  images top left hand corner will be aligned with the
     *                  top left hand corner of this cell.
     * @param sheet A reference to the sheet that contains the cell identified
     *              by the two parameters above.
     * @param drawing An instance of the DrawingPatriarch class. This is now
     *                passed into the method where it was, previously, recovered
     *                from the sheet in order to allow multiple pictures be
     *                inserted. If the patriarch was not 'cached in this manner
     *                each time it was created any previously positioned images
     *                would be simply over-written.
     * @param imageFile An instance of the URL class that encapsulates the name
     *                  of and path to the image that is to be 'inserted into'
     *                  the sheet.
     * @param reqImageWidthMM A primitive double that contains the required
     *                        width of the image in millimetres.
     * @param reqImageHeightMM A primitive double that contains the required
     *                         height of the image in millimetres.
     * @param resizeBehaviour A primitive int whose value will determine how
     *                        the code should react if the image is larger than
     *                        the cell referenced by the colNumber and
     *                        rowNumber parameters. Four constants are provided
     *                        to determine what should happen;
     *                          AddDimensionedImage.EXPAND_ROW
     *                          AddDimensionedImage.EXPAND_COLUMN
     *                          AddDimensionedImage.EXPAND_ROW_AND_COLUMN
     *                          AddDimensionedImage.OVERLAY_ROW_AND_COLUMN
     * @throws java.io.FileNotFoundException If the file containing the image
     *                                       cannot be located.
     * @throws java.io.IOException If a problem occurs whilst reading the file
     *                             of image data.
     * @throws java.lang.IllegalArgumentException If an invalid value is passed
     *                                            to the resizeBehaviour
     *                                            parameter or if the extension
     *                                            of the image file indicates that
     *                                            it is of a type that cannot
     *                                            currently be added to the worksheet.
     */
    public void addImage(int colNumber, int rowNumber, Sheet sheet, Drawing drawing,
    		String imageFile, double reqImageWidthMM, double reqImageHeightMM,
            DimensionedImageType resizeBehaviour) throws IOException,
                                                     IllegalArgumentException ;


    public void addImage(int colNumber, int rowNumber,
    		String imageFile, double reqImageWidthMM, double reqImageHeightMM,
            DimensionedImageType resizeBehaviour) throws IOException, IllegalArgumentException ;
    
    /**
     * Add an image to a worksheet.
     *
     * @param cellNumber A String that contains the location of the cell whose
     *                   top left hand corner should be aligned with the top
     *                   left hand corner of the image; for example "A1", "A2"
     *                   etc. This is to support the familiar Excel syntax.
     *                   Whilst images are are not actually inserted into cells
     *                   this provides a convenient method of indicating where
     *                   the image should be positioned on the sheet.
     * @param sheet A reference to the sheet that contains the cell referenced
     *              above.
     * @param drawing An instance of the DrawingPatriarch class. This is now
     *                passed into the method where it was, previously, recovered
     *                from the sheet in order to allow multiple pictures be
     *                inserted. If the patriarch was not 'cached in this manner
     *                each time it was created any previously positioned images
     *                would be simply over-written.
     * @param imageFile An instance of the URL class that encapsulates the name
     *                  of and path to the image that is to be 'inserted into'
     *                  the sheet.
     * @param reqImageWidthMM A primitive double that contains the required
     *                        width of the image in millimetres.
     * @param reqImageHeightMM A primitive double that contains the required
     *                         height of the image in millimetres.
     * @param resizeBehaviour A primitive int whose value will determine how
     *                        the code should react if the image is larger than
     *                        the cell referenced by the cellNumber parameter.
     *                        Four constants are provided to determine what
     *                        should happen;
     *                          AddDimensionedImage.EXPAND_ROW
     *                          AddDimensionedImage.EXPAND_COLUMN
     *                          AddDimensionedImage.EXPAND_ROW_AND_COLUMN
     *                          AddDimensionedImage.OVERLAY_ROW_AND_COLUMN
     * @throws java.io.FileNotFoundException If the file containing the image
     *                                       cannot be located.
     * @throws java.io.IOException If a problem occurs whilst reading the file
     *                             of image data.
     * @throws java.lang.IllegalArgumentException If an invalid value is passed
     *                                            to the resizeBehaviour
     *                                            parameter.
     */
    public void addImage(String cellNumber, Sheet sheet, Drawing drawing,
    		String imageFile, double reqImageWidthMM, double reqImageHeightMM,
            DimensionedImageType resizeBehaviour) throws IOException, IllegalArgumentException;
    
    
    public void addImage(String cellNumber,
    		String imageFile, double reqImageWidthMM, double reqImageHeightMM,
            DimensionedImageType resizeBehaviour) throws IOException, IllegalArgumentException;

    
    public CellStyle getTitleStyle();
    
    public CellStyle getHeadStyle();
    
    public CellStyle getBodyStyle();

    public Cell addCell(final Row row, final int colIndex,final CellStyle sytle, final int type, final Object value);

    public Cell addCell(final Row row, final int colIndex,final int type, final Object value);
    
    
    public Cell addCell(final int rowIndex, final int colIndex,final int type, final Object value);
    
    public Cell addCell(final Row row, final int colIndex, final Object value);
    
    public Row addRow(final int rowIndex, final List<Object> list);
    
    public Row addRow(final int rowIndex, final int startColIndex, final List<Object> list);
    
    public Row createHead(final int rowIndex, final int startColIndex, final String ...heads);
    
    public Row createTitle(final int rowIndex, final int startColIndex, final int endColIndex, final String title);

	public <T> void export(String string, String[] headers, Collection<T> dataset);
    
	public <T> void export(String title, String[] headers, Collection<T> dataset
			,int rowIndex, final int startColIndex);
	
	public <T> void export(String string, String[] headers, Collection<T> dataset,FieldMapper mapper);
    
	public <T> void export(String title, String[] headers, Collection<T> dataset
			,int rowIndex, final int startColIndex,FieldMapper mapper);
	
	public void scan(int sheetNum,int rowStartIndex,int startColIndex,int endColIndex, RowColMapper mapper);
	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月14日 下午7:34:52
	 * @version V1.0
	 * @param rowIndex
	 * @param colIndex
	 * @param value
	 * @return
	 */
	Cell addCell(int rowIndex, int colIndex, Object value);


	public interface RowColMapper{
		void mapper(int rowIndex, int colIndex, Object value);
		
	}


	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月14日 下午10:15:48
	 * @version V1.0
	 * @param sheetNum
	 * @param rowStartIndex
	 * @param startColIndex
	 * @param mapper
	 */
	void scan(int sheetNum, int rowStartIndex, int startColIndex,
			RowColMapper mapper);

	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月15日 下午8:10:00
	 * @version V1.0
	 * @param rowStartIndex
	 * @param startColIndex
	 * @param mapper
	 */
	void scan(int rowStartIndex, int startColIndex, RowColMapper mapper);

	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月15日 下午9:00:46
	 * @version V1.0
	 * @param rowIndex
	 * @param startColIndex
	 * @param endColIndex
	 * @return
	 */
	Map<Integer, Object> getRowMap(int rowIndex, int startColIndex,
			int endColIndex);

	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月15日 下午9:00:52
	 * @version V1.0
	 * @param sheet
	 * @param rowIndex
	 * @param startColIndex
	 * @param endColIndex
	 * @return
	 */
	Map<Integer, Object> getRowMap(Sheet sheet, int rowIndex,
			int startColIndex, int endColIndex);

	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月15日 下午9:00:59
	 * @version V1.0
	 * @param rowIndex
	 * @return
	 */
	Map<Integer, Object> getRowMap(int rowIndex);

	
	/**
	 * 
	 * @author lisheng
	 * @date 2016年6月15日 下午9:01:03
	 * @version V1.0
	 * @param sheet
	 * @param rowIndex
	 * @return
	 */
	Map<Integer, Object> getRowMap(Sheet sheet, int rowIndex);
}
